package clubhouse.control;

import java.util.ListIterator;
import java.util.Vector;

/**
 * @author Anis Darwich & Lionel Croix - Simulation and experimentation on Robot in WSN - WnSN Lab - SJTU
 */
public class ErrorRetriever {

	/**Pool of randomly generated error.*/
	private Vector<double[]> errorpool;
	
	/**
	 * Constructs the ErrorRetriever object, instantiate its 2 vectors.
	 */
	public ErrorRetriever() {
		errorpool = new Vector<double[]>(100, 100);
	}

	/**
	 * Generates a pool of 2000 random values to be used while calculating final error.
	 */
	public void generateErrorPool() {
		int i;
		int step = 0;
		for(i = 0; i<2000; i++) {
			double err = (Math.random()*2-1);
			errorpool.add(new double [] {step, err});
			step++;
		}
	}
	
	/**
	 * Returns the value from the generated pool and corresponding to the given step.
	 * @param step the step during which the asked error will be applied.
	 * @return the double array of error to applied [X coordinate error,Y coordinate error] according to
	 * the specified step, if found, [0,0] otherwise.
	 */
	public double[] getErrorFromPool(int step) {
		double[] temp = null, result = null;
        ListIterator<double[]> li = this.errorpool.listIterator();
        while(li.hasNext()){
        	temp = li.next();
        	if (temp[0] == step) {
        		result = temp;
        		break;
        	}
        }
        if (result == null) {
			result = new double[] {0, 0};
		}
		return result;
	}
	
}
